<template>
  <div class="review-container">
	  <button class="back-btn" @click="goBack">← 返回</button>
	  
	  
    <!-- 评论列表 -->
    <h2 class="title">用户评论（共{{ reviews.length }}条）</h2>
    <div v-if="reviews.length === 0" class="empty-tip">暂无评论，快来发表第一条评论吧~</div>
    <ul v-else class="review-list">
      <li v-for="review in reviews" :key="review.reviewId" class="review-item">
        <div class="user-info">
          <span class="username">{{ user.userName || '匿名用户' }}</span>
          
        </div>
        <div class="review-content">{{ review.content }}</div>
        <button 
          v-if="review.userId === user.userId"
          class="delete-btn"
          @click="handleDelete(review.reviewId)"
        >
          删除
        </button>

      </li>
    </ul>

  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'FoodReview',
  data() {
    return {
      reviews: [],      // 评论列表
      newReview: '',    // 新评论
      user: {},         // 用户信息
      foodId: null      // 食物ID
    };
  },
  created() {
    this.foodId = this.$route.query.foodId;
	this.user = this.$getSessionStorage('user') || {}; // 确保 this.user 不为空
    this.checkLogin();
    this.loadReviews();
  },
  methods: {
    // 登录验证（保持不变）
    checkLogin() { 
      /* 登录验证的逻辑 */
    },goBack() {
	    this.$router.go(-1) // 路由回退
	  },

    // 加载评论
    loadReviews() {
      this.$axios.get('/Review/ReviewListOfUser', {
        params: { userId: this.user.userId }
      })
      .then((res) => {
        if (res.data.result) { // 使用 result 判断是否成功
          this.reviews = res.data.result;
        } else {
          alert('评论为空');
        }
      })
      .catch((error) => {
        alert('评论为空');
      });
    },
    // 删除评论
    handleDelete(reviewId) {
        this.$axios.delete('/Review/ReviewId', {
          params: { reviewId:reviewId}
        })
        .then((res) => {
          if (res.data.result) {
			   this.loadReviews();  // 使用 result 判断是否成功
            alert('删除成功');
          } else {
           alert('删除失败');
          }
        })
        .catch((error) => {
          this.$message.error('删除失败');
          console.error('删除错误:', error);
        });
      
    }

  }
};
</script>

<style scoped>
	.back-btn {
	  padding: 8px 16px;
	  margin-bottom: 20px;
	  background: #f5f5f5;
	  color: #666;
	  border: 1px solid #ddd;
	  border-radius: 4px;
	  cursor: pointer;
	  transition: all 0.3s;
	}
	
	.back-btn:hover {
	  background: #2d8cf0;
	  color: white;
	  border-color: #2d8cf0;
	  transform: translateY(-1px);
	  box-shadow: 0 2px 8px rgba(45, 140, 240, 0.2);
	}
.review-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 30px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.08);
}

.title {
  font-size: 20px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #eee;
}

/* 评论列表 */
.review-list {
  margin: 30px 0;
}

.review-item {
  position: relative;
  padding: 20px;
  margin-bottom: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  transition: all 0.3s;

  &:hover {
    transform: translateX(10px);
    box-shadow: 2px 2px 8px rgba(0,0,0,0.1);
  }
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 10px;

  .username {
    font-weight: 500;
    color: #2d8cf0;
    margin-right: 15px;
  }

  .time {
    font-size: 12px;
    color: #999;
  }
}

.review-content {
  line-height: 1.6;
  color: #666;
}

/* 删除按钮 */
.delete-btn {
  position: absolute;
  right: 15px;
  top: 15px;
  padding: 5px;
  color: #ff4d4f;
  background: none;
  border: none;
  cursor: pointer;
  opacity: 0.6;
  transition: all 0.3s;

  &:hover {
    opacity: 1;
    transform: scale(1.1);
  }

  .iconfont {
    font-size: 16px;
  }
}

/* 发表评论 */
.post-review {
  margin-top: 40px;
}

.input-area {
  position: relative;
}

.review-input {
  width: 100%;
  height: 100px;
  padding: 15px;
  border: 2px solid #e8e8e8;
  border-radius: 8px;
  resize: none;
  font-size: 14px;
  transition: border-color 0.3s;

  &:focus {
    border-color: #2d8cf0;
    outline: none;
  }
}

.action {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;

  .word-count {
    font-size: 12px;
    color: #999;
  }
}

.submit-btn {
  padding: 8px 25px;
  background: #2d8cf0;
  color: #fff;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s;

  &:disabled {
    background: #c5c8ce;
    cursor: not-allowed;
  }

  &:not(:disabled):hover {
    opacity: 0.9;
    transform: translateY(-1px);
  }
}

.empty-tip {
  text-align: center;
  color: #999;
  padding: 40px 0;
}
</style>
